import pandas as pd
url ="C:\python\penguin.csv"
df = pd.read_csv(url)
# 打印数据集的前5行
print(df.head())

# Let's visualize the distribution of the penguins species with a bar plot in matplotlib
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 指定CSV文件路径
file_path = "C:\\python\\penguin.csv"

# 加载数据
df = pd.read_csv(file_path)

# 使用Seaborn设置图表风格
sns.set(style="whitegrid")

# 计算每个种类的企鹅数量
species_counts = df['Species'].value_counts()

# 创建柱状图
plt.figure(figsize=(10, 6))  # 设置图形大小
sns.barplot(x=species_counts.index, y=species_counts.values)

# 添加标题和轴标签
plt.title('Distribution of Penguin Species')
plt.xlabel('Species')
plt.ylabel('Counts')

# 显示图表
plt.show()
# Let's visualize with boxplots how the FlipperLength, CulmenLength and CulmenDepth are distributed for each species

# 创建一个画布，并设置大小
plt.figure(figsize=(10, 6))

# 绘制箱线图
# 定义测量值列表
measurements = ['FlipperLength', 'CulmenLength', 'CulmenDepth']

# 为每个测量值绘制箱线图
for measurement in measurements:
    plt.figure(figsize=(10, 6))
    sns.boxplot(x='Species', y=measurement, data=df, palette='deep', width=0.6)
    plt.title(f'Boxplot of {measurement} by Species')
    plt.xlabel('Species')
    plt.ylabel(measurement)
    plt.show()

# 添加交互式图例
handles, labels = plt.gca().get_legend_handles_labels()
by_species = plt.legend(handles[0:], labels[0:], title='Measurements', loc='upper right', bbox_to_anchor=(1.15, 1))

# 添加标题和轴标签
plt.title('Boxplot of Penguin Measurements by Species')
plt.xlabel('Species')
plt.ylabel('Measurement (mm)')

# 显示图表
plt.show()

# Show rows with missing values
# 显示包含缺失值的行
# 使用 isnull() 函数来查找缺失值，然后通过 any(axis=1) 检查每行是否包含至少一个缺失值
missing_values_rows = df[df.isnull().any(axis=1)]

# 打印包含缺失值的行
print("Rows with missing values:")
print(missing_values_rows)
# Drop rows with missing values
# 删除包含缺失值的行
# 这个方法会返回一个新的DataFrame，其中不包含任何有缺失值的行
df_dropped = df.dropna()

# 显示结果，确认缺失值已被删除
print(df_dropped)
# Let's prepare for training:
# 1. Split the data into features and labels
# 2. Split the data into training and test sets

# Split the data into features and labels
# features are CulmenLength, CulmenDepth, FlipperLength
# labels are Species
# Split the data into training and test sets in a way to have 30% of the data for testing
# Let's train a Logistic Regression model
# 1. Create a multiclass Logistic Regression model
# 2. Train the model

# Create a multiclass Logistic Regression model
# Let's evaluate the model
# 1. Predict the labels of the test set
# 2. Calculate the accuracy of the model